 package com.xy.p2p.action.front;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import sun.security.krb5.internal.Ticket;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.security.encoder.PwdEncoder;
import com.xy.common.util.DictUtils;
import com.xy.common.util.EmailTools;
import com.xy.common.util.ErFen;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TmemberassetaccountService;

@Controller
public class TmemberAction {
	public static final String AUTH_KEY = " ";
	public static final String MODIFYPWD_SUCCESS = "success";
	private static final Logger log = LoggerFactory
			.getLogger(TmemberAction.class);

	public TmemberAction() {
	}

//	@RequestMapping(value = "/member/tomembercenter.jspx")
//	public String toMemberCenter(HttpServletRequest request,
//			HttpServletResponse response,Tmember tmember, ModelMap model){
//		return "/front/member/index.html";
//	}
	
//	@RequestMapping(value = "/member/v_tmemberTomemberinfo.jspx")
	@RequestMapping(value = "/member/tomembercenter.jspx")
	public String tomemberinfo(HttpServletRequest request,
			HttpServletResponse response,Tmember tmember, ModelMap model){
		if(null==request.getSession().getAttribute(AUTH_KEY))
			return "redirct:/login.jspx";
		Tmember member = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		setDict(model);
		model.addAttribute("user",member);
//		return "/front/member/memberInfo.html";
		//用于导航栏
		model.addAttribute("dis", 13);
		return "/front/member/index.html";
	}
	
	
	
	//修改密码
	@RequestMapping(value = "member/pwd.jspx")
	public String toModifyPwd(HttpServletRequest request,
			HttpServletResponse response,Tmember tmember, ModelMap model){
		if(null!= request.getSession().getAttribute("pwdmsg")){
			model.addAttribute("message", request.getSession().getAttribute("pwdmsg").toString());
		}
		request.getSession().setAttribute("pwdmsg",null);
		return "/front/member/modifyPwd.html";
	}
	
	/*//修改密码提交
	@RequestMapping(value = "/member/pwdSubmit.jspx")
	public String submitModifyPwd(HttpServletRequest request,
			HttpServletResponse response, Tmember tmember, ModelMap model) {
		Tmember member = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		String newpwd = request.getParameter("newpwd");
		String newpwdConfirm = request.getParameter("newpwdconfirm"); 
		String msg = tmemberService.modifyPWD(member.getMemberid(),request.getParameter("pwd"), newpwd);
		if (MODIFYPWD_SUCCESS != msg) {
			request.getSession().setAttribute("pwdmsg", msg);
			return "redirect:/member/pwd.jspx";
		}
		if(!newpwd.equals(newpwdConfirm)){
			request.getSession().setAttribute("pwdmsg", "新密码不一致");
			return "redirect:/member/pwd.jspx";
		}
		request.getSession().invalidate();
		return  "redirect:/login.jspx";
	}*/
	
	//修改密码提交 ajax
	@RequestMapping(value = "/member/pwdSubmit.jspx")
	public void submitModifyPwd(HttpServletRequest request,
			HttpServletResponse response, Tmember tmember, ModelMap model) {
		Tmember member = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		String newpwd = request.getParameter("newpwd");
		String newpwdConfirm = request.getParameter("newpwdconfirm"); 
		String msg = tmemberService.modifyPWD(member.getMemberid(),request.getParameter("pwd"), newpwd);
		try {
			response.setContentType("text/Xml;charset=gbk");
			PrintWriter out = response.getWriter();
			if("true".equals(msg))
				request.getSession().invalidate();
			out.print(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//修改提现密码提交 ajax
		@RequestMapping(value = "/member/o_tmemberSubmitModifyTPwd.jspx")
		public void submitModifyTPwd(HttpServletRequest request,
 HttpServletResponse response, Tmember tmember, ModelMap model) {
		Tmember member = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		String newpwd = request.getParameter("newtpwd");
		String newpwdConfirm = request.getParameter("newtpwdconfirm");
		String msg = tmemberService.modifyTPWD(member.getMemberid(), request.getParameter("tpwd"), newpwd);
		request.getSession().setAttribute(AUTH_KEY, tmemberService.get(member.getMemberid()));
		try {
			response.setContentType("text/Xml;charset=gbk");
			PrintWriter out = response.getWriter();
			out.print(msg);
			if("true".equals(msg)){
//				request.getSession().invalidate();
				request.getSession().setAttribute(AUTH_KEY, tmemberService.get(tmember.getMemberid()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	@RequestMapping(value = "/member/memberinfosubmit.jspx")
	public String memberinfoSubmit(HttpServletRequest request,HttpServletResponse response,Tmember tmember,ModelMap model,String captcha){
		tmember.setModtime(new Date());
		WebErrors errors = validateLogin(tmember.getLoginname(),
				tmember.getPasswd(), captcha, null, request, response);
		if (errors.hasErrors()) {
			setDict(model);
			model.addAttribute("member", tmember);
			errors.toModel(model);
			return "/front/member/memberinfoEdit.html";
		}
		setDict(model);
		try {
			request.getSession().setAttribute(TmemberAction.AUTH_KEY, tmemberService.save(tmember));
			return "redirect:/member/tomembercenter.jspx";
		} catch (Exception e) {
			e.printStackTrace();
			return "error.html";
		}
	}
	@RequestMapping(value = "/member/toMemberinfoEdit.jspx")
	public String toMemberinfoEdit(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		try {
		Tmember auth = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		setDict(model);
			model.addAttribute("member",auth);
			return "/front/member/memberinfoEdit.html";
		} catch (Exception e) {
			e.printStackTrace();
			//用于导航栏
			model.addAttribute("dis", 13);
			return "error.html";
		}
	}
	
	@RequestMapping(value = "/member/logout.jspx")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) {
		Tmember authId = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		if (authId != null) {
			request.getSession().invalidate();
		}
		String returnUrl = RequestUtils.getQueryParam(request, "returnUrl");
		if (!StringUtils.isBlank(returnUrl)) {
			return "redirect:" + returnUrl;
		} else {
			return "redirect:login.jspx";
		}
	}

	@RequestMapping(value = "/member/register.jspx", method = RequestMethod.GET)
	public String register(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
//		model.addAttribute("test", "ming");
		model.addAttribute("tmember", new Tmember());
		setDict(model);
		return "regist.html";
	}

	@RequestMapping(value = "/member/register.jspx", method = RequestMethod.POST)
	public String registerSubmit(Tmember tmember, String captcha,
			String returnUrl, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		setDict(model);
		WebErrors errors = validateLogin(tmember.getLoginname(),
				tmember.getPasswd(), captcha, null, request, response);
		if("".equals(tmember.getLoginname())||"".equals(tmember.getPasswd())){
			errors.addError("用户名或密码不能为空");
		}
		if (!errors.hasErrors()) {
			String ip = RequestUtils.getIpAddr(request);
			tmember.setRegip(ip);
			Tmember temp = tmemberService.save(tmember);
			request.getSession().setAttribute("tempTmember", tmemberService.get(temp.getMemberid()));
			
			return "redirect:/member/login.jspx";
		}
		// 校验失败
		errors.toModel(model);
		model.addAttribute("success", true);
		model.addAttribute("tmember", tmember);
		if (returnUrl != null) {
			return "redirect:" + returnUrl;
		} else {
			return "regist.html";
		}
	}

	@RequestMapping(value = "/member/login.jspx", method = RequestMethod.GET)
	public String login(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String returnUrl = RequestUtils.getQueryParam(request, "returnUrl");
		//从cookies获取用户名(记住用户功能)
		for(Cookie c : request.getCookies()){
			if(c.getName().equals("loginTmember")){
				Tmember temp = new Tmember();
				temp.setLoginname(c.getValue());
				model.addAttribute("tempTmember", temp);
				model.addAttribute("flag", true);
			}
		}
		if (request.getSession().getAttribute(AUTH_KEY) != null) {
			if (!StringUtils.isBlank(returnUrl)) {
				return "redirect:" + returnUrl;
			} else {
				return "redirect:/index.jspx";
			}
		}
		if(null!=request.getSession().getAttribute("tempTmember")){
			model.addAttribute("tempTmember", request.getSession().getAttribute("tempTmember"));
			model.addAttribute("tishi", "注册成功，请输入密码登录");
		}
		return "login.html";
	}

	@RequestMapping(value = "/member/login.jspx", method = RequestMethod.POST)
	public String loginSubmit(String loginname, String password,
			String captcha, HttpServletRequest request,
 HttpServletResponse response, ModelMap model, Boolean flag) {

		// 从cookies获取用户名(记住用户功能)
		for (Cookie c : request.getCookies()) {
			if (c.getName().equals("loginTmember")) {
				Tmember temp = new Tmember();
				temp.setLoginname(c.getValue());
				model.addAttribute("tempTmember", temp);
				model.addAttribute("flag", true);
			}
		}
		if (null != flag && flag) {
			model.addAttribute("flag", true);
		}
		// end
		// WebErrors errors = validateLogin(loginname, password, captcha, null,
		// request, response);
		// if (!errors.hasErrors()) {
		WebErrors errors = WebErrors.create(request);
		try {
			Tmember user = tmemberService.login(request, loginname, password);
			// String returnUrl = model.get("returnUrl").toString();

			try {
				Cookie localCookie = new Cookie("jforumSSOCookieNameUser", URLEncoder.encode(user.getLoginname() + "," + user.getPasswd() + "," + user.getLoginname(), "utf-8"));
				localCookie.setMaxAge(86400);
				localCookie.setPath("/");
				response.addCookie(localCookie);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 记住账户功能
			if (null != flag && flag) {
				Cookie remeberFlag = new Cookie("loginTmember", loginname);
				remeberFlag.setMaxAge(86400);
				response.addCookie(remeberFlag);
			}
			return "redirect:/member/tomembercenter.jspx";
		} catch (UsernameNotFoundException e) {
			errors.addErrorString(e.getMessage());
			e.printStackTrace();
		} catch (BadCredentialsException e) {
			errors.addErrorString(e.getMessage());
			e.printStackTrace();
		}
		// }
		errors.toModel(model);
		// 如果登录错误, 带回刚才填写的登录名回去页面显示
		Tmember temp = new Tmember();
		temp.setLoginname(loginname);
		model.addAttribute("tempTmember", temp);
		return "login.html";
	}

	@RequestMapping(value = "/member/index.jspx", method = RequestMethod.GET)
	public String index(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {

		Tmember tmember = (Tmember) request.getSession().getAttribute(AUTH_KEY);
		model.addAttribute("user", tmember);
		return "/front/member/index.html";
	}

	private WebErrors validateLogin(String loginname, String passwd,
			String captcha, Integer errorRemaining, HttpServletRequest request,
 HttpServletResponse response) {
		WebErrors errors = WebErrors.create(request);
		// if (errors.ifOutOfLength(loginname, "loginname", 1, 20)) {
		// return errors;
		// }
		// if (errors.ifOutOfLength(passwd, "passwd", 1, 32)) {
		// return errors;
		// }
		// 如果输入了验证码，那么必须验证；如果没有输入验证码，则根据当前用户判断是否需要验证码。
		if (!StringUtils.isBlank(captcha) || (errorRemaining != null && errorRemaining < 0)) {
			if (errors.ifBlank(captcha, "captcha", 100)) {
				return errors;
			}
			try {
				if (!imageCaptchaService.validateResponseForID(request.getSession().getId(), captcha)) {
					errors.addError("验证码错误");
					return errors;
				}
			} catch (CaptchaServiceException e) {
				errors.addError("验证码无效");
				log.warn("", e);
				return errors;
			}
		}
		return errors;
	}

	/**
	 * 会员信息新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_tmemberAdd.jspx")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmember", new Tmember());
		setDict(model);
		return "tmemberEdit";
	}

	/**
	 * 会员信息新增保存
	 * 
	 * @param tmember
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/o_tmemberSave.jspx")
	public String save(Tmember tmember, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateSave(tmember, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmember = tmemberService.save(tmember);
		return "redirect:v_tmemberList.jspx";
	}

	/**
	 * 会员信息编辑页面
	 * 
	 * @param memberid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_tmemberEdit.jspx")
	public String edit(long memberid, HttpServletRequest request, ModelMap model) {
		Tmember tmember = tmemberService.get(memberid);
		model.addAttribute("tmember", tmember);
		setDict(model);
		return "tmemberEdit";
	}

	/**
	 * 会员信息详情
	 * 
	 * @param memberid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_tmemberDetail.jspx")
	public String detail(long memberid, HttpServletRequest request,
			ModelMap model) {
		Tmember tmember = tmemberService.get(memberid);
		model.addAttribute("tmember", tmember);
		setDict(model);
		return "tmemberView";
	}

	//安全中心
	@RequestMapping(value = "/member/v_tmemberToSecurity.jspx")
	public String toSecurity(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		Tmember tmember = (Tmember)request.getSession().getAttribute(AUTH_KEY);
		model.addAttribute("tmember", tmember);
		//用于导航栏
		model.addAttribute("dis", 14);
		return "/front/member/safety_install.html";
	}
	
	@RequestMapping(value = "/member/upload/Head.jspx")
	public String Head(MultipartFile file,HttpServletRequest request,HttpServletResponse response,ModelMap model){
		Tmember tmember = (Tmember)request.getSession().getAttribute(AUTH_KEY);
		model.addAttribute("tmember", tmember);
		return "/front/member/safety_install.html";
	}

	//忘记密码
	@RequestMapping(value = "/o_tmemberGetPWD.jspx")
	public void GetPWD(ModelMap model, HttpServletRequest request,
			HttpServletResponse response, String loginname) throws Exception {
//		response.setContentType("text/xml;charset=UTF-8");
//		response.setHeader("Pragma", "No-cache");
//		response.setHeader("Cache-Control", "no-cache");
//		response.setDateHeader("Expires", 0);
		response.setCharacterEncoding("UTF-8");
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("loginname", loginname);
		model.addAttribute("loginname", loginname);
		List<Tmember> tList = tmemberService.getTmember(queryMap);
		try {
			if (tList.size() <= 0) {
				response.getWriter().write("用户名不存在.");
				return;
			}
			Tmember tmember = tList.get(0);
			if (null == tmember.getEmail() || "".equals(tmember.getEmail())) {
				response.getWriter().write("该用户没有设置安全邮箱.");
				return;
			}
			// 生成验证码
			String vCode = "";
			Cookie cookie = CookieUtils.getCookie(request, loginname);
//			if (null != request.getSession().getAttribute("vCode")) {
			if(null!=cookie){
				vCode = cookie.getValue();
			} else {
				vCode = ErFen.getRamdonNum(6) + "";
				cookie = new Cookie(loginname, vCode);
				cookie.setMaxAge(1800);
				response.addCookie(cookie);	
//				request.getSession().setAttribute("vCode", vCode);
			}
			String content = "尊敬的用户：\n您好，您在贷一贷网站申请找回密码操作的验证码为:" + vCode
					+ "\n	为保障您的帐号安全性，验证码有效期为30分钟，验证成功后自动失效。\n注：此邮件由系统自动发送，请勿回复如果您有任何疑问，请您访问http://www.51d1d.com/help/helpinvest.jspx，或\r联系客服电话400-1122-363\n\n\n\n\n\n\n\t贷一贷团队";
			EmailTools.sendEmail(content, "贷一贷找回密码", tmember.getEmail().trim());
			response.getWriter().write("true");
		} catch (Exception e) {
			log.error(e.toString());
			throw e;
		}
	}
	//跳转到忘记密码页面
	@RequestMapping(value = "/o_tmemberToGetPWD.jspx")
	public String ToGetPWD(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return "/front/forgetPWD.html";
	}
	
	//验证验证码(忘记密码)
	@RequestMapping(value = "/o_tmembervalidateVCode.jspx")
	public String validateVCode(ModelMap model,HttpServletRequest request,String vCode,String loginname){
		model.addAttribute("loginname", loginname);
		Cookie cookie = CookieUtils.getCookie(request, loginname);
		if(null == cookie){
			model.addAttribute("error", "验证码超时, 请重新操作");
			return "/front/forgetPWD.html";
		}
//		if(vCode.equals(request.getSession().getAttribute("vCode"))){
		if(vCode.equals(cookie.getValue())){
			request.getSession().setAttribute("vCode",null);
			return "/front/resetPWD.html";
		}else{
			model.addAttribute("error", "验证码不正确");
			return "/front/forgetPWD.html";
		}
	}

	// 忘记密码
	@RequestMapping(value = "/forgetpwd.jspx")
	public String submit(HttpServletRequest request, HttpServletResponse response,String loginname, String PWD, ModelMap model) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("loginname", loginname);
		Tmember tmember = tmemberService.getTmember(map).get(0);
		tmember.setPasswd(pwdEncoder.encodePassword(PWD));
		tmember = tmemberService.save(tmember);
		model.addAttribute("tempTmember", tmember);
		return "login.html";
	}
	
	/**
	 * 发送验证邮箱链接
	 * */
	@RequestMapping("/o_sendverify.jspx")
	public String sendverify(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		Tmember tmember = (Tmember)request.getSession().getAttribute(AUTH_KEY);
		tmemberService.sendEmail(tmember);
		try {
			response.setCharacterEncoding("GBK");
			response.getWriter().write("<script type='text/javascript'>alert('已发送验证邮箱链接...')</script>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 model.addAttribute("Tmember", tmember);
//		 model.addAttribute("opr","1");
		 model.addAttribute("dis", 14);
		return "/front/member/safety_install.html";
	}
	/**
	 * 验证邮箱链接
	 * */
	@RequestMapping("/o_verify.jspx")
	public String verify(HttpServletResponse response,HttpServletRequest request, ModelMap model){
		response.setCharacterEncoding("GBK");
		//得到邮件链接传过来的值
		String account = request.getParameter("account");
		String email = request.getParameter("email");
		String type = request.getParameter("type");
		String check = request.getParameter("check");
		//check是memberid 得到tmember
		
		try {
			Tmember tmember = tmemberService.get(Long.valueOf(check));
			//判断邮件传过来的值和用户的信息是否一致
			if(pwdEncoder.isPasswordValid(account, tmember.getLoginname(),"dai")
					&&pwdEncoder.isPasswordValid(email, tmember.getEmail(), "yi")
					&&pwdEncoder.isPasswordValid(type, tmember.getEmailstatus().toString(), "dai")){
				//如果是将邮件状态设置为激活
				tmember.setEmailstatus(1);
				tmemberService.save(tmember);
				model.addAttribute("message","验证成功，请登录后使用...");
				return "error/error.html";
			//如果其他信息正确，邮箱状态不正确，则邮箱已经验证过
			}else if(pwdEncoder.isPasswordValid(account, tmember.getLoginname(),"dai")
					&&pwdEncoder.isPasswordValid(email, tmember.getEmail(), "yi")
					&&!pwdEncoder.isPasswordValid(type, tmember.getEmailstatus().toString(), "dai")){
				model.addAttribute("message","你的邮箱已验证，请登录后使用...");
				return "error/error.html";
			}else{
				model.addAttribute("message","验证失败,链接不在有效期...");
				return "error/error.html";
			}
		} catch (Exception e) {
			model.addAttribute("message","验证失败...");
			return "error/error.html";
		}
	}
	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmember
	 * @return
	 */
	private Map<String, Object> toMap(Tmember tmember) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (tmember.getMemberid() != null && !tmember.getMemberid().equals("")) {
			map.put("memberid", tmember.getMemberid());
		}
		if (tmember.getLoginname() != null
				&& !tmember.getLoginname().equals("")) {
			map.put("loginname", tmember.getLoginname());
		}
		if (tmember.getPasswd() != null && !tmember.getPasswd().equals("")) {
			map.put("passwd", tmember.getPasswd());
		}
		if (tmember.getUsertype() != null && !tmember.getUsertype().equals("")) {
			map.put("usertype", tmember.getUsertype());
		}
		if (tmember.getTruename() != null && !tmember.getTruename().equals("")) {
			map.put("truename", tmember.getTruename());
		}
		if (tmember.getSex() != null && !tmember.getSex().equals("")) {
			map.put("sex", tmember.getSex());
		}
		if (tmember.getIdcard() != null && !tmember.getIdcard().equals("")) {
			map.put("idcard", tmember.getIdcard());
		}
		if (tmember.getBirthday() != null && !tmember.getBirthday().equals("")) {
			map.put("birthday", tmember.getBirthday());
		}
		if (tmember.getPhone() != null && !tmember.getPhone().equals("")) {
			map.put("phone", tmember.getPhone());
		}
		if (tmember.getEmail() != null && !tmember.getEmail().equals("")) {
			map.put("email", tmember.getEmail());
		}
		if (tmember.getEdudegree() != null
				&& !tmember.getEdudegree().equals("")) {
			map.put("edudegree", tmember.getEdudegree());
		}
		if (tmember.getGraduated() != null
				&& !tmember.getGraduated().equals("")) {
			map.put("graduated", tmember.getGraduated());
		}
		if (tmember.getMarriage() != null && !tmember.getMarriage().equals("")) {
			map.put("marriage", tmember.getMarriage());
		}
		if (tmember.getAddr() != null && !tmember.getAddr().equals("")) {
			map.put("addr", tmember.getAddr());
		}
		if (tmember.getWorktype() != null && !tmember.getWorktype().equals("")) {
			map.put("worktype", tmember.getWorktype());
		}
		if (tmember.getCorpindustry() != null
				&& !tmember.getCorpindustry().equals("")) {
			map.put("corpindustry", tmember.getCorpindustry());
		}
		if (tmember.getCorpsize() != null && !tmember.getCorpsize().equals("")) {
			map.put("corpsize", tmember.getCorpsize());
		}
		if (tmember.getPosition() != null && !tmember.getPosition().equals("")) {
			map.put("position", tmember.getPosition());
		}
		if (tmember.getIncome() != null && !tmember.getIncome().equals("")) {
			map.put("income", tmember.getIncome());
		}
		if (tmember.getRegtime() != null && !tmember.getRegtime().equals("")) {
			map.put("regtime", tmember.getRegtime());
		}
		if (tmember.getModtime() != null && !tmember.getModtime().equals("")) {
			map.put("modtime", tmember.getModtime());
		}
		if (tmember.getRegip() != null && !tmember.getRegip().equals("")) {
			map.put("regip", tmember.getRegip());
		}
		if (tmember.getLogtimes() != null && !tmember.getLogtimes().equals("")) {
			map.put("logtimes", tmember.getLogtimes());
		}
		if (tmember.getErrtimes() != null && !tmember.getErrtimes().equals("")) {
			map.put("errtimes", tmember.getErrtimes());
		}
		if (tmember.getLastloginip() != null
				&& !tmember.getLastloginip().equals("")) {
			map.put("lastloginip", tmember.getLastloginip());
		}
		if (tmember.getLastlogintime() != null
				&& !tmember.getLastlogintime().equals("")) {
			map.put("lastlogintime", tmember.getLastlogintime());
		}
		if (tmember.getActivationcode() != null
				&& !tmember.getActivationcode().equals("")) {
			map.put("activationcode", tmember.getActivationcode());
		}
		if (tmember.getStatus() != null && !tmember.getStatus().equals("")) {
			map.put("status", tmember.getStatus());
		}
		if (tmember.getEmail() != null && !tmember.getStatus().equals("")){
			map.put("emailstatus", tmember.getEmailstatus());
		}
		return map;
	}

	/**
	 * 设置页面所需的数据字典
	 * 
	 * @param model
	 */
	private void setDict(ModelMap model){
	      model.addAttribute("usertypeMap", DictUtils.getDictByType(DictUtils.TMEMBER_USERTYPE));
	      model.addAttribute("sexMap", DictUtils.getDictByType(DictUtils.TMEMBER_SEX));
	      model.addAttribute("edudegreeMap", DictUtils.getDictByType(DictUtils.TMEMBER_EDUDEGREE));
	      model.addAttribute("marriageMap", DictUtils.getDictByType(DictUtils.TMEMBER_MARRIAGE));
	      model.addAttribute("worktypeMap", DictUtils.getDictByType(DictUtils.TMEMBER_WORKTYPE));
	      model.addAttribute("corpsizeMap", DictUtils.getDictByType(DictUtils.TMEMBER_CORPSIZE));
	      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TMEMBER_STATUS));
	      model.addAttribute("cbcreditMap", DictUtils.getDictByType(DictUtils.TMEMBER_CBCREDIT));
	      model.addAttribute("fixedassetsMap", DictUtils.getDictByType(DictUtils.TMEMBER_FIXEDASSETS));
	      model.addAttribute("currentassetsMap", DictUtils.getDictByType(DictUtils.TMEMBER_CURRENTASSETS));	
	      model.addAttribute("emailstatusMap",DictUtils.getDictByType(DictUtils.TMEMBER_EMAILSTATUS));
		}

	/**
	 * 数据校验
	 * 
	 * @param model
	 */
	private WebErrors validateSave(Tmember tmember, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**
		 * if (errors.ifOutOfLength(username, "username", 1, 100)) { return
		 * errors; }
		 */
		return errors;
	}

	
	/**
	 * 检查登陆名是否已经存在
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/member/check_loginname.jspx")
	public void check_loginname(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			String loginname = request.getParameter("loginname");
			if (loginname == null || loginname.equals("")) {
				response.getWriter().write("false");
				return;
			}
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("loginname", loginname);
			List l = tmemberService.getTmember(param);
			String text = l.size() == 0 ? "true" : "false";
			response.setContentType("text/xml;charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(text);
		} catch (Exception e) {
			log.error(e.toString());
			throw e;
		}
	}
	@Resource
	private ImageCaptchaService imageCaptchaService;
	@Resource
	private TmemberService tmemberService;
	@Resource
	private TmemberassetaccountService tmemberassetaccountService;
	@Resource
	private PwdEncoder pwdEncoder;
}